package com.hoshiicloud.goods.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hoshiicloud.common.aop.Logable;
import com.hoshiicloud.common.exception.CommonError;
import com.hoshiicloud.common.exception.ServiceException;
import com.hoshiicloud.common.query.OmnipotentParam;
import com.hoshiicloud.common.query.OmnipotentWrapper;
import com.hoshiicloud.goods.entity.goods.Gallery;
import com.hoshiicloud.goods.entity.sku.BaseSku;
import com.hoshiicloud.goods.entity.sku.BaseSkuGallery;
import com.hoshiicloud.goods.entity.spu.BaseGoodsGallery;
import com.hoshiicloud.goods.feign.IdGeneratorService;
import com.hoshiicloud.goods.mapper.sku.BaseSkuGalleryMapper;
import com.hoshiicloud.goods.mapper.spu.BaseGoodsGalleryMapper;
import com.hoshiicloud.goods.service.BaseGoodsGalleryService;
import com.hoshiicloud.goods.service.BaseSkuGalleryService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * BaseSpu图片资源表
 * </p>
 *
 * @author zhangshuhu
 * @since 2019-06-04
 */
@Slf4j
@Service
public class BaseGoodsGalleryServiceImpl extends ServiceImpl<BaseGoodsGalleryMapper, BaseGoodsGallery> implements BaseGoodsGalleryService {

    @Autowired
    private IdGeneratorService idGeneratorService;

    /**
     *  baseSpu新增相册关联关系
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(cacheNames = "BaseGoodsGallery", allEntries = true)
    @Logable(authName = "baseSpu新增相册关联关系")
    public boolean save(BaseGoodsGallery entity) {

        // 生成主键id
        String id = idGeneratorService.queryGeneratorId();
        if (StringUtils.isBlank(id)) {
            throw new ServiceException(CommonError.error("新增失败"));
        }

        // 保存数据库
        entity.setId(Long.parseLong(id));
        entity.setCreateTime(LocalDateTime.now());

        int row = baseMapper.insert(entity);
        if (row <= 0) {
            throw new ServiceException(CommonError.error("新增失败"));
        }
        return true;
    }

    /**
     * 批量新增相册信息
     */
    @Override
    @CacheEvict(cacheNames = "BaseGoodsGallery", allEntries = true)
    @Logable(authName = "批量新增BaseSpu相册信息")
    public boolean saveBaseGoodsGalleryBatch(List<BaseGoodsGallery> galleries) {
        boolean isTrue = this.saveBatch(galleries);
        if (!isTrue) {
            throw new ServiceException("批量新增BaseSpu相册信息失败！");
        }
        return true;
    }

    /**
     * 批量删除: 物理删除
     */
    @Override
    @CacheEvict(cacheNames = "BaseGoodsGallery", allEntries = true)
    @Logable(authName = "批量删除: 物理删除")
    public Boolean removeBatch(List<OmnipotentWrapper> param) {
        QueryWrapper<BaseGoodsGallery> wrapper = new OmnipotentWrapper<BaseGoodsGallery>().getWrapper(param);
        return this.remove(wrapper);
    }

    @Override
    @CacheEvict(cacheNames = "BaseGoodsGallery", allEntries = true)
    @Logable(authName = "批量更新baseSpu相册信息")
    public Boolean updateBatch(OmnipotentParam<BaseGoodsGallery> param) {
        List<OmnipotentWrapper> wrappers = param.getWrappers();
        UpdateWrapper<BaseGoodsGallery> updateWrapper = new OmnipotentWrapper<BaseGoodsGallery>().getUpdateWrapper(wrappers);
        BaseGoodsGallery entity = param.getEntity();
        return this.update(entity, updateWrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(cacheNames = "BaseGoodsGallery", allEntries = true)
    @Logable(authName = "根据Id删除baseSpu相册信息")
    public boolean deleteById(Long id) {

        // 根据id查询该数据是否存在
        BaseGoodsGallery entity = this.getById(id);
        if (entity == null) {
            throw new ServiceException("根据Id删除baseSpu相册异常，无法找到基础SKU商品图片资源记录");
        }

        // 进行删除操作
        entity.setUpdateTime(LocalDateTime.now());
        entity.setDeleted(1);
        int row = baseMapper.updateById(entity);
        if (row <= 0) {
            throw new ServiceException(CommonError.error("删除失败"));
        }
        return true;
    }

    /**
     * 通过baseSpuId查询相册信息
     */
    @Override
    public List<BaseGoodsGallery> listBySpuId(Long baseSpuId) {

        QueryWrapper<BaseGoodsGallery> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(BaseGoodsGallery::getGoodsId, baseSpuId)
                        .eq(BaseGoodsGallery::getDeleted, 0);

        List<BaseGoodsGallery> baseGoodsGalleries = baseMapper.selectList(wrapper);
        return baseGoodsGalleries;
    }

}
